<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="版本号" prop="versionNo">
        <el-input
          v-model="queryParams.versionNo"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="算法类型" prop="algorithmType">
        <el-input
          v-model="queryParams.algorithmType"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList">
      <el-table-column
        prop="versionNo"
        label="版本号"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="algorithmType"
        label="算法类型"
        align="center"
      ></el-table-column>
      <el-table-column
        label="数据集id"
        align="center"
        prop="dataSetId"
      ></el-table-column>
      <el-table-column
        label="训练样本数"
        align="center"
        prop="trainSampleNum"
      ></el-table-column>
      <el-table-column
        label="训练样本时间"
        align="center"
        prop="trainSampleTime"
      ></el-table-column>
      <el-table-column
        label="版本固化时间"
        align="center"
        prop="versionFreezeTime"
      ></el-table-column>
      <el-table-column
        label="模型正确率"
        align="center"
        prop="accuracy"
      ></el-table-column>
      <el-table-column
        label="版本描述"
        align="center"
        prop="description"
      ></el-table-column>
      <el-table-column label="模型地址" align="center" prop="files">
        <template slot-scope="scope">
          <div v-for="(item, index) in scope.row.files" :key="index">
            <!-- <a :href="item.url" :download="item.name">{{item.name}}</a> -->
            <a>{{ item.name }}</a>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="是否使用" align="center" prop="inUse">
        <template slot-scope="scope">
          <span>{{ scope.row.inUse ? "使用" : "未使用" }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleUpdate(scope.row)"
            >编辑</el-button
          >
          <el-button size="mini" type="text" @click="handleVision(scope.row)"
            >上装</el-button
          >
          <el-button
            v-if="scope.row.parentId != 0"
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改部门对话框 -->
    <Edit
      v-if="dialogVisible"
      :dialogTitle="dialogTitle"
      :dialogOpen="dialogOpen"
      :dialogData="dialogData"
      @closeDialog="closeDialog"
    />
  </div>
</template>

<script>
import {
  getVersionList,
  removeVersion,
  queryAllDataSource,
  publishVersion,
} from "@/api/aifactory";
import Edit from "./components/edit.vue";

export default {
  name: "Datasource",
  components: { Edit },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 表格树数据
      dataList: [],
      // 重新渲染表格状态
      refreshTable: true,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        versionNo: undefined,
        algorithmType: undefined,
      },
      id: undefined,
      sourceCodeDatas: [],
      //弹窗参数
      dialogVisible: false,
      dialogTitle: "",
      dialogOpen: false,
      dialogData: {},
    };
  },
  created() {
    this.getList();
    //获取数据源
    queryAllDataSource().then((res) => {
      if (res.code == 200) {
        this.sourceCodeDatas = res.data;
        console.log("sourceCodeDatas:", this.sourceCodeDatas);
      }
    });
  },
  mounted() {},
  methods: {
    getList() {
      this.id = parseInt(this.$route.query.id);
      this.loading = true;
      getVersionList(this.id, this.queryParams).then((response) => {
        response.data.forEach((item) => {
          let files = [];
          console.log(217, JSON.parse(item?.versionFiles));
          if (JSON.parse(item?.versionFiles).length > 0) {
            JSON.parse(item.versionFiles).forEach((item1) => {
              item1.name = item1.newFileName;
              item1.url = item1.filePath;
              // item1.url = window.location.origin + window.location.pathname + '/'+item1.filePath
              files.push(item1);
            });
            item.files = files;
          } else {
            item.files = [];
          }
        });
        console.log(222, response.data);
        this.dataList = response.data;
        // this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.deptId,
        label: node.deptName,
        children: node.children,
      };
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    //关闭库相关
    closeDialog() {
      this.dialogVisible = false;
      this.dialogOpen = false;
      this.getList();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.dialogOpen = true;
      this.dialogTitle = "新建模型信息";
      this.dialogVisible = true;
      this.dialogData = {};
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(218, row);
      this.dialogOpen = true;
      this.dialogTitle = "编辑数据源";
      this.dialogVisible = true;
      this.dialogData = row;
    },
    //版本上装
    handleVision(row) {
      console.log(230, row);
      this.$modal
        .confirm("确定上装该版本吗？")
        .then(function () {
          return publishVersion(row.modelId, row.id);
        })
        .then(() => {
          this.$message({ message: '上装成功', type: "success" });
        })
        .catch(() => {
          this.$message({ message:'上装失败，请重试', type: "error" });
        });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      console.log(254, row);
      this.$modal
        .confirm('确定删除该项吗？')
        .then(function () {
          return removeVersion([row.id]);
        })
        .then(() => {
          this.$modal.msgSuccess("删除成功");
          this.getList();
        })
        .catch(() => {
          this.$modal.msgError("删除失败");
        });
    },
  },
};
</script>
